package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zch.house.entity.FlatRepair;
import com.zch.house.service.FlatRepairService;
import com.zch.house.service.impl.FlatRepairServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class FlatRepairServlet
 */
@WebServlet("/FlatRepairServlet2")
public class FlatRepairServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    FlatRepairService frs = new FlatRepairServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FlatRepairServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("gfrbi".equals(method)){
			getFlatRepairById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String flat_idStr = request.getParameter("flatId");
		int flat_id = Integer.parseInt(flat_idStr);
		String lo_idStr = request.getParameter("loId");
		int lo_id = Integer.parseInt(lo_idStr);
		String fr_explain = request.getParameter("explain");
		String fr_timeStr = request.getParameter("time");
		Date fr_time = DateFormat.to_Date2(fr_timeStr);
		String fr_result = request.getParameter("result");
		String fr_remark = request.getParameter("remark");
		FlatRepair flatRepair = new FlatRepair(flat_id, lo_id, fr_explain, fr_time, fr_result, fr_remark);
		boolean flag = frs.addFlatRepair(flatRepair);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='FlatRepairServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='FlatRepairServlet2?method=select'</script>");
		}
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fr_idStr = request.getParameter("id");
		int fr_id = Integer.parseInt(fr_idStr);
		boolean flag = frs.deleteFlatRepair(fr_id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='FlatRepairServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='FlatRepairServlet2?method=select'</script>");
		}
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fr_idStr = request.getParameter("id");
		int fr_id = Integer.parseInt(fr_idStr);
		String flat_idStr = request.getParameter("flatId");
		int flat_id = Integer.parseInt(flat_idStr);
		String lo_idStr = request.getParameter("loId");
		int lo_id = Integer.parseInt(lo_idStr);
		String fr_explain = request.getParameter("explain");
		String fr_timeStr = request.getParameter("time");
		Date fr_time = DateFormat.to_Date2(fr_timeStr);
		String fr_result = request.getParameter("result");
		String fr_remark = request.getParameter("remark");
		FlatRepair flatRepair = new FlatRepair(fr_id, flat_id, lo_id, fr_explain, fr_time, fr_result, fr_remark);
		boolean flag = frs.updateFlatRepair(flatRepair);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='FlatRepairServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='FlatRepairServlet2?method=select'</script>");
		}
		
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String area = request.getParameter("area"); 
		String estate = request.getParameter("estate");
		String unitNumberStr = request.getParameter("unitNumber");
		String idStr = request.getParameter("id");
		if(unitNumberStr==null||unitNumberStr==""){
			unitNumberStr = "-1";
		}
		int unitNumber = Integer.parseInt(unitNumberStr); 
		String floorStr = request.getParameter("floor");
		if(floorStr==null||floorStr==""){
			floorStr = "-1";
		}
		int floor = Integer.parseInt(floorStr);
		String startDateStr = request.getParameter("startDate");
		Date startDate = DateFormat.to_Date2(startDateStr);
		String endDateStr = request.getParameter("endDate");
		Date endDate = DateFormat.to_Date2(endDateStr);
		String pageIndexStr = request.getParameter("pageIndex");
		if((pageIndexStr==null)||(pageIndexStr=="")){
			pageIndexStr = "1";
		}
		int pageIndex = Integer.parseInt(pageIndexStr);
		//调用service层方法
		List<FlatRepair> list = frs.getAllFlatRepairByPage(area, estate, unitNumber, 
				floor, startDate, endDate, pageIndex);
		HttpSession session = request.getSession();
		//详情
		if(idStr!=null&&idStr!=""){
			int id = Integer.parseInt(idStr);
			@SuppressWarnings("unchecked")
			List<FlatRepair> list2 = (List<FlatRepair>) session.getAttribute("list");
			FlatRepair flatRepair = list2.get(id);
			request.setAttribute("flatRepair", flatRepair);
			request.getRequestDispatcher("view/flatrepair/detail.jsp").forward(request, response);
			return;
		}
		
		//获得总页码
		int totalPages = frs.getTotalCount(area, estate, unitNumber, floor, startDate, endDate);
		//将参数存入request域
		request.setAttribute("area", area);
		request.setAttribute("estate", estate);
		if(unitNumber==-1){
			request.setAttribute("unitNumber", null);
		}else{
			request.setAttribute("unitNumber", unitNumber);
		}
		
		if(floor==-1){
			request.setAttribute("floor", null);
		}else{
			request.setAttribute("floor", floor);
		}
		
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("totalPages", totalPages);
		session.setAttribute("list", list);
		request.getRequestDispatcher("view/flatrepair/list.jsp").forward(request, response);
	}

	private void getFlatRepairById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String fr_idStr = request.getParameter("id");
		int fr_id = Integer.parseInt(fr_idStr);
		FlatRepair flatRepair = frs.getFlatRepairById(fr_id);
		request.setAttribute("flatRepair", flatRepair);
		request.getRequestDispatcher("view/flatrepair/edit.jsp").forward(request, response);		
	}

}
